package com.jdk8;

import com.pojo.Student;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 数组和集合转 Stream
 */
public class StreamAPI {
    public static void main(String[] args) {

         //把数组里面的所有偶数找出来放到list集合,并打印
         int[] arr = {2,1,4,3,6,12,5,8}; //2,4,6,12,8

         //filter就是过滤，在原来的基础上去查找
         //如果要修改原来的内容就是映射map
         //排序sort（年龄最小)
         //最终只有1个limit
        //把数组转为Stream（开始)，再过滤(中间操作)，再转为数组(结果)
         int[] newArray = Arrays.stream(arr).filter(n->n%2==0).map(n->n*2).sorted().limit(1).toArray();

         //数组转为Stream遍历
         Arrays.stream(newArray).forEach(System.out::println);

         //-----------------------------------------------------
        System.out.println("------------------------------------");
        List<Integer> oldList = Arrays.asList(1,2,3,4,5,6,0);
        List  list = oldList.stream().filter(n->n%2==1).map(n->n*2).sorted().collect(Collectors.toList());
        list.forEach(System.out::println);

        System.out.println("----------------------------------");
        Student s1 = new Student(1,"aaa");
        Student s2 = new Student(3,"ccc");
        Student s3 = new Student(2,"bbb");
        List<Student> listStu = Arrays.asList(s1,s2,s3); //.filter(s->s.getSid()%2==1)
        List<Student> list2 = listStu.stream().sorted(Comparator.comparing(Student::getSid)).collect(Collectors.toList());
        Collections.reverse(list2);//倒序
        list2.forEach(System.out::println);

        //Stream.of(1,2,3,4);





    }
}
